#ifndef __VV_VVM_HPP__
#define __VV_VVM_HPP__

#include <sstream>
#include <QXmlAttributes>
#include <QXmlDefaultHandler>
#include <QXmlParseException>
#include <util/undeletable_ptr.hpp>
#include "vertex_factory.hpp"
#include "vset.hpp"

namespace vv {
  /** @brief A class for writing a vertex line to a vvm file */
  class vvm_handler : public QXmlDefaultHandler {
  public:
    vvm_handler();
    ~vvm_handler();

    void init(vv::vertex_factory* f, vv::vset* s, bool read_global = true);

    bool fatalError(const QXmlParseException& e);

    bool startElement(const QString&, const QString&, const QString& qName, const QXmlAttributes& atts);
    bool endDocument();

  protected:
    virtual void vprop(vv::vptr v, std::string name, std::istringstream& value) = 0;
    virtual void eprop(vv::vptr first, vv::vptr second, bool symmetric, std::string name, std::string value) = 0;
    virtual void gprop(std::string name, std::string value) = 0;

  private:
    typedef std::vector<std::pair<std::string, std::string> > pvec;
    struct vnb {
      vv::vptr v;
      std::list<unsigned int> nb;
    };
    struct e {
      bool         symmetric;
      unsigned int first;
      unsigned int second;
      pvec         properties;
    };

    vv::vertex_factory* f;
    vv::vset*           s;
    bool                g;
    std::vector<vnb>    vertices;
    std::vector<e>      edges;
  };

  void read_vvm(util::undeletable_ptr<vv::vvm_handler> r, std::string filename);
}

#endif
